{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 时间日期\n",
    "\n",
    "* 时间戳 tiimestamp：固定的时刻 -> pd.Timestamp\n",
    "* 固定时期 period：比如 2016年3月份，再如2015年销售额 -> pd.Period\n",
    "* 时间间隔 interval：由起始时间和结束时间来表示，固定时期是时间间隔的一个特殊\n",
    "\n",
    "**时间日期在 Pandas 里的作用**\n",
    "\n",
    "* 分析金融数据，如股票交易数据\n",
    "* 分析服务器日志\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python datetime\n",
    "\n",
    "python 标准库里提供了时间日期的处理。这个是时间日期的基础。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "from datetime import timedelta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2016, 4, 28, 14, 49, 51, 307000)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now = datetime.now()\n",
    "now"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2016, 4, 28)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.year, now.month, now.day"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 时间差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.timedelta(4)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date1 = datetime(2016, 3, 20)\n",
    "date2 = datetime(2016, 3, 16)\n",
    "delta = date1 - date2\n",
    "delta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delta.days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "345600.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delta.total_seconds()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2016, 3, 20, 0, 0)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date2 + delta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2016, 3, 20, 12, 0)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date2 + timedelta(4.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串和 datetime 转换\n",
    "\n",
    "关于 datetime 格式定义，可以参阅 python 官方文档"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2016, 3, 20, 8, 30)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date = datetime(2016, 3, 20, 8, 30)\n",
    "date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2016-03-20 08:30:00'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2016-03-20 08:30:00'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date.strftime('%Y-%m-%d %H:%M:%S')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2016, 3, 20, 9, 30)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.strptime('2016-03-20 09:30', '%Y-%m-%d %H:%M')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pandas 里的时间序列\n",
    "\n",
    "Pandas 里使用 Timestamp 来表达时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-03-01    1.650889\n",
       "2016-03-02   -0.328463\n",
       "2016-03-03    1.674872\n",
       "2016-03-04   -0.310849\n",
       "dtype: float64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dates = [datetime(2016, 3, 1), datetime(2016, 3, 2), datetime(2016, 3, 3), datetime(2016, 3, 4)]\n",
    "s = pd.Series(np.random.randn(4), index=dates)\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.tseries.index.DatetimeIndex"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(s.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.tslib.Timestamp"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(s.index[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 日期范围"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 生成日期范围"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-03-20', '2016-03-21', '2016-03-22', '2016-03-23',\n",
       "               '2016-03-24', '2016-03-25', '2016-03-26', '2016-03-27',\n",
       "               '2016-03-28', '2016-03-29', '2016-03-30', '2016-03-31'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('20160320', '20160331')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-03-20', '2016-03-21', '2016-03-22', '2016-03-23',\n",
       "               '2016-03-24', '2016-03-25', '2016-03-26', '2016-03-27',\n",
       "               '2016-03-28', '2016-03-29'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(start='20160320', periods=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-03-20', '2016-03-21', '2016-03-22', '2016-03-23',\n",
       "               '2016-03-24', '2016-03-25', '2016-03-26', '2016-03-27',\n",
       "               '2016-03-28', '2016-03-29'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 规则化时间戳\n",
    "pd.date_range(start='2016-03-20 16:23:32', periods=10, normalize=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 时间频率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-03-20', '2016-03-27', '2016-04-03', '2016-04-10',\n",
       "               '2016-04-17', '2016-04-24', '2016-05-01', '2016-05-08',\n",
       "               '2016-05-15', '2016-05-22'],\n",
       "              dtype='datetime64[ns]', freq='W-SUN')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 星期\n",
    "pd.date_range(start='20160320', periods=10, freq='W')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-03-31', '2016-04-30', '2016-05-31', '2016-06-30',\n",
       "               '2016-07-31', '2016-08-31', '2016-09-30', '2016-10-31',\n",
       "               '2016-11-30', '2016-12-31'],\n",
       "              dtype='datetime64[ns]', freq='M')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 月\n",
    "pd.date_range(start='20160320', periods=10, freq='M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-03-31', '2016-04-29', '2016-05-31', '2016-06-30',\n",
       "               '2016-07-29', '2016-08-31', '2016-09-30', '2016-10-31',\n",
       "               '2016-11-30', '2016-12-30'],\n",
       "              dtype='datetime64[ns]', freq='BM')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 每个月最后一个工作日组成的索引\n",
    "pd.date_range(start='20160320', periods=10, freq='BM')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-03-20 00:00:00', '2016-03-20 04:00:00',\n",
       "               '2016-03-20 08:00:00', '2016-03-20 12:00:00',\n",
       "               '2016-03-20 16:00:00', '2016-03-20 20:00:00',\n",
       "               '2016-03-21 00:00:00', '2016-03-21 04:00:00',\n",
       "               '2016-03-21 08:00:00', '2016-03-21 12:00:00'],\n",
       "              dtype='datetime64[ns]', freq='4H')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 小时\n",
    "pd.date_range(start='20160320', periods=10, freq='4H')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 时期及算术运算\n",
    "\n",
    "pd.Period 表示时期，比如几日，月或几个月等。比如用来统计每个月的销售额，就可以用时期作为单位。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2010', 'A-DEC')"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p1 = pd.Period(2010)\n",
    "p1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2012', 'A-DEC')"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p2 = p1 + 2\n",
    "p2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2L"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p2 - p1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-01', 'M')"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p1 = pd.Period(2016, freq='M')\n",
    "p1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-04', 'M')"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p1 + 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 时期序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2016-01', '2016-02', '2016-03', '2016-04', '2016-05', '2016-06',\n",
       "             '2016-07', '2016-08', '2016-09', '2016-10', '2016-11', '2016-12'],\n",
       "            dtype='int64', freq='M')"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.period_range(start='2016-01', periods=12, freq='M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2016-01', '2016-02', '2016-03', '2016-04', '2016-05', '2016-06',\n",
       "             '2016-07', '2016-08', '2016-09', '2016-10'],\n",
       "            dtype='int64', freq='M')"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.period_range(start='2016-01', end='2016-10', freq='M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2016Q1', '2016Q2', '2016Q3'], dtype='int64', freq='Q-DEC')"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 直接用字符串\n",
    "index = pd.PeriodIndex(['2016Q1', '2016Q2', '2016Q3'], freq='Q-DEC')\n",
    "index"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 时期的频率转换\n",
    "\n",
    "asfreq\n",
    "\n",
    "* A-DEC: 以 12 月份作为结束的年时期\n",
    "* A-NOV: 以 11 月份作为结束的年时期\n",
    "* Q-DEC: 以 12 月份作为结束的季度时期\n",
    "\n",
    "![time freq](imgs/time_freq.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016', 'A-DEC')"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = pd.Period('2016', freq='A-DEC')\n",
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-01', 'M')"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p.asfreq('M', how='start')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-12', 'M')"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p.asfreq('M', how='end')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-04', 'M')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = pd.Period('2016-04', freq='M')\n",
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016', 'A-DEC')"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p.asfreq('A-DEC')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2017', 'A-MAR')"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 以年为周期，以一年中的 3 月份作为年的结束（财年）\n",
    "p.asfreq('A-MAR')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 季度时间频率\n",
    "\n",
    "Pandas 支持 12 种季度型频率，从 Q-JAN 到 Q-DEC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016Q4', 'Q-JAN')"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = pd.Period('2016Q4', 'Q-JAN')\n",
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Period('2015-11-01', 'D'), Period('2016-01-31', 'D'))"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 以 1 月份结束的财年中，2016Q4 的时期是指 2015-11-1 到 2016-1-31\n",
    "p.asfreq('D', how='start'), p.asfreq('D', how='end')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-01-28 16:00', 'T')"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取该季度倒数第二个工作日下午4点的时间戳\n",
    "p4pm = (p.asfreq('B', how='end') - 1).asfreq('T', 'start') + 16 * 60\n",
    "p4pm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2016-01-28 16:00:00')"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转换为 timestamp\n",
    "p4pm.to_timestamp()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Timestamp 和 Period 相互转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-01-31   -0.773323\n",
       "2016-02-29    0.215953\n",
       "2016-03-31    1.301631\n",
       "2016-04-30   -0.066134\n",
       "2016-05-31    1.651792\n",
       "Freq: M, dtype: float64"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts = pd.Series(np.random.randn(5), index = pd.date_range('2016-01-01', periods=5, freq='M'))\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-01   -0.773323\n",
       "2016-02    0.215953\n",
       "2016-03    1.301631\n",
       "2016-04   -0.066134\n",
       "2016-05    1.651792\n",
       "Freq: M, dtype: float64"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.to_period()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-12-29   -0.110462\n",
       "2016-12-30   -0.265792\n",
       "2016-12-31   -0.382456\n",
       "2017-01-01   -0.036111\n",
       "2017-01-02   -1.029658\n",
       "Freq: D, dtype: float64"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts = pd.Series(np.random.randn(5), index = pd.date_range('2016-12-29', periods=5, freq='D'))\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-12   -0.110462\n",
       "2016-12   -0.265792\n",
       "2016-12   -0.382456\n",
       "2017-01   -0.036111\n",
       "2017-01   -1.029658\n",
       "Freq: M, dtype: float64"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pts = ts.to_period(freq='M')\n",
    "pts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-12   -0.758711\n",
       "2017-01   -1.065769\n",
       "Freq: M, dtype: float64"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pts.groupby(level=0).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-12-31   -0.110462\n",
       "2016-12-31   -0.265792\n",
       "2016-12-31   -0.382456\n",
       "2017-01-31   -0.036111\n",
       "2017-01-31   -1.029658\n",
       "dtype: float64"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转换为时间戳时，细部时间会丢失\n",
    "pts.to_timestamp(how='end')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 重采样\n",
    "\n",
    "* 高频率 -> 低频率 -> 降采样：5 分钟股票交易数据转换为日交易数据\n",
    "* 低频率 -> 高频率 -> 升采样\n",
    "* 其他重采样：每周三 (W-WED) 转换为每周五 (W-FRI)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-04-25 09:30:00    18\n",
       "2016-04-25 09:31:00    41\n",
       "2016-04-25 09:32:00    49\n",
       "2016-04-25 09:33:00    26\n",
       "2016-04-25 09:34:00     5\n",
       "2016-04-25 09:35:00    12\n",
       "2016-04-25 09:36:00     6\n",
       "2016-04-25 09:37:00    47\n",
       "2016-04-25 09:38:00    16\n",
       "2016-04-25 09:39:00    37\n",
       "2016-04-25 09:40:00    44\n",
       "2016-04-25 09:41:00     8\n",
       "2016-04-25 09:42:00    22\n",
       "2016-04-25 09:43:00    24\n",
       "2016-04-25 09:44:00    12\n",
       "2016-04-25 09:45:00    26\n",
       "2016-04-25 09:46:00    30\n",
       "2016-04-25 09:47:00    38\n",
       "2016-04-25 09:48:00     5\n",
       "2016-04-25 09:49:00    26\n",
       "2016-04-25 09:50:00    39\n",
       "2016-04-25 09:51:00     7\n",
       "2016-04-25 09:52:00     6\n",
       "2016-04-25 09:53:00    12\n",
       "2016-04-25 09:54:00    24\n",
       "2016-04-25 09:55:00     0\n",
       "2016-04-25 09:56:00    12\n",
       "2016-04-25 09:57:00    27\n",
       "2016-04-25 09:58:00    10\n",
       "2016-04-25 09:59:00    26\n",
       "2016-04-25 10:00:00    27\n",
       "2016-04-25 10:01:00    18\n",
       "2016-04-25 10:02:00    27\n",
       "2016-04-25 10:03:00    25\n",
       "2016-04-25 10:04:00    25\n",
       "2016-04-25 10:05:00    35\n",
       "2016-04-25 10:06:00    28\n",
       "2016-04-25 10:07:00     3\n",
       "2016-04-25 10:08:00    20\n",
       "2016-04-25 10:09:00    48\n",
       "2016-04-25 10:10:00     5\n",
       "2016-04-25 10:11:00    48\n",
       "2016-04-25 10:12:00    30\n",
       "2016-04-25 10:13:00     2\n",
       "2016-04-25 10:14:00    11\n",
       "2016-04-25 10:15:00    18\n",
       "2016-04-25 10:16:00    21\n",
       "2016-04-25 10:17:00    32\n",
       "2016-04-25 10:18:00    43\n",
       "2016-04-25 10:19:00    10\n",
       "2016-04-25 10:20:00     5\n",
       "2016-04-25 10:21:00    45\n",
       "2016-04-25 10:22:00     3\n",
       "2016-04-25 10:23:00    30\n",
       "2016-04-25 10:24:00     3\n",
       "2016-04-25 10:25:00    24\n",
       "2016-04-25 10:26:00    46\n",
       "2016-04-25 10:27:00     2\n",
       "2016-04-25 10:28:00    33\n",
       "2016-04-25 10:29:00    25\n",
       "Freq: T, dtype: int32"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts = pd.Series(np.random.randint(0, 50, 60), index=pd.date_range('2016-04-25 09:30', periods=60, freq='T'))\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-04-25 09:30:00    139\n",
       "2016-04-25 09:35:00    118\n",
       "2016-04-25 09:40:00    110\n",
       "2016-04-25 09:45:00    125\n",
       "2016-04-25 09:50:00     88\n",
       "2016-04-25 09:55:00     75\n",
       "2016-04-25 10:00:00    122\n",
       "2016-04-25 10:05:00    134\n",
       "2016-04-25 10:10:00     96\n",
       "2016-04-25 10:15:00    124\n",
       "2016-04-25 10:20:00     86\n",
       "2016-04-25 10:25:00    130\n",
       "Freq: 5T, dtype: int32"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 0-4 分钟为第一组\n",
    "ts.resample('5min', how='sum')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-04-25 09:35:00    139\n",
       "2016-04-25 09:40:00    118\n",
       "2016-04-25 09:45:00    110\n",
       "2016-04-25 09:50:00    125\n",
       "2016-04-25 09:55:00     88\n",
       "2016-04-25 10:00:00     75\n",
       "2016-04-25 10:05:00    122\n",
       "2016-04-25 10:10:00    134\n",
       "2016-04-25 10:15:00     96\n",
       "2016-04-25 10:20:00    124\n",
       "2016-04-25 10:25:00     86\n",
       "2016-04-25 10:30:00    130\n",
       "Freq: 5T, dtype: int32"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 0-4 分钟为第一组\n",
    "ts.resample('5min', how='sum', label='right')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### OHLC 重采样\n",
    "\n",
    "金融数据专用：Open/High/Low/Close"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-04-25 09:30:00</th>\n",
       "      <td>18</td>\n",
       "      <td>49</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 09:35:00</th>\n",
       "      <td>12</td>\n",
       "      <td>47</td>\n",
       "      <td>6</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 09:40:00</th>\n",
       "      <td>44</td>\n",
       "      <td>44</td>\n",
       "      <td>8</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 09:45:00</th>\n",
       "      <td>26</td>\n",
       "      <td>38</td>\n",
       "      <td>5</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 09:50:00</th>\n",
       "      <td>39</td>\n",
       "      <td>39</td>\n",
       "      <td>6</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 09:55:00</th>\n",
       "      <td>0</td>\n",
       "      <td>27</td>\n",
       "      <td>0</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 10:00:00</th>\n",
       "      <td>27</td>\n",
       "      <td>27</td>\n",
       "      <td>18</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 10:05:00</th>\n",
       "      <td>35</td>\n",
       "      <td>48</td>\n",
       "      <td>3</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 10:10:00</th>\n",
       "      <td>5</td>\n",
       "      <td>48</td>\n",
       "      <td>2</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 10:15:00</th>\n",
       "      <td>18</td>\n",
       "      <td>43</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 10:20:00</th>\n",
       "      <td>5</td>\n",
       "      <td>45</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25 10:25:00</th>\n",
       "      <td>24</td>\n",
       "      <td>46</td>\n",
       "      <td>2</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     open  high  low  close\n",
       "2016-04-25 09:30:00    18    49    5      5\n",
       "2016-04-25 09:35:00    12    47    6     37\n",
       "2016-04-25 09:40:00    44    44    8     12\n",
       "2016-04-25 09:45:00    26    38    5     26\n",
       "2016-04-25 09:50:00    39    39    6     24\n",
       "2016-04-25 09:55:00     0    27    0     26\n",
       "2016-04-25 10:00:00    27    27   18     25\n",
       "2016-04-25 10:05:00    35    48    3     48\n",
       "2016-04-25 10:10:00     5    48    2     11\n",
       "2016-04-25 10:15:00    18    43   10     10\n",
       "2016-04-25 10:20:00     5    45    3      3\n",
       "2016-04-25 10:25:00    24    46    2     25"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('5min', how='ohlc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-03-01    13\n",
       "2016-03-02    21\n",
       "2016-03-03    26\n",
       "2016-03-04     3\n",
       "2016-03-05    31\n",
       "2016-03-06    29\n",
       "2016-03-07    42\n",
       "2016-03-08    24\n",
       "2016-03-09    10\n",
       "2016-03-10    42\n",
       "2016-03-11    42\n",
       "2016-03-12     7\n",
       "2016-03-13    10\n",
       "2016-03-14    48\n",
       "2016-03-15    12\n",
       "2016-03-16    15\n",
       "2016-03-17    16\n",
       "2016-03-18    34\n",
       "2016-03-19    45\n",
       "2016-03-20    40\n",
       "2016-03-21    45\n",
       "2016-03-22    46\n",
       "2016-03-23    21\n",
       "2016-03-24    27\n",
       "2016-03-25    10\n",
       "2016-03-26    47\n",
       "2016-03-27     8\n",
       "2016-03-28     9\n",
       "2016-03-29     0\n",
       "2016-03-30    20\n",
       "              ..\n",
       "2016-05-10    38\n",
       "2016-05-11    46\n",
       "2016-05-12     8\n",
       "2016-05-13    15\n",
       "2016-05-14    13\n",
       "2016-05-15    30\n",
       "2016-05-16    25\n",
       "2016-05-17    15\n",
       "2016-05-18     3\n",
       "2016-05-19     5\n",
       "2016-05-20    21\n",
       "2016-05-21    18\n",
       "2016-05-22    11\n",
       "2016-05-23    47\n",
       "2016-05-24    14\n",
       "2016-05-25    33\n",
       "2016-05-26    37\n",
       "2016-05-27    40\n",
       "2016-05-28     5\n",
       "2016-05-29    27\n",
       "2016-05-30     2\n",
       "2016-05-31    31\n",
       "2016-06-01    31\n",
       "2016-06-02    41\n",
       "2016-06-03    28\n",
       "2016-06-04     2\n",
       "2016-06-05    21\n",
       "2016-06-06    10\n",
       "2016-06-07    21\n",
       "2016-06-08    18\n",
       "Freq: D, dtype: int32"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 通过 groupby 重采样\n",
    "ts = pd.Series(np.random.randint(0, 50, 100), index=pd.date_range('2016-03-01', periods=100, freq='D'))\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3    759\n",
       "4    648\n",
       "5    748\n",
       "6    172\n",
       "dtype: int32"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.groupby(lambda x: x.month).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-03    759\n",
       "2016-04    648\n",
       "2016-05    748\n",
       "2016-06    172\n",
       "Freq: M, dtype: int32"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.groupby(ts.index.to_period('M')).sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 升采样和插值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-04-22</th>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-29</th>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             0\n",
       "2016-04-22  10\n",
       "2016-04-29   6"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 以周为单位，每周五采样\n",
    "df = pd.DataFrame(np.random.randint(1, 50, 2), index=pd.date_range('2016-04-22', periods=2, freq='W-FRI'))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-04-22</th>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-23</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-24</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-26</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-27</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-28</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-29</th>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             0\n",
       "2016-04-22  10\n",
       "2016-04-23 NaN\n",
       "2016-04-24 NaN\n",
       "2016-04-25 NaN\n",
       "2016-04-26 NaN\n",
       "2016-04-27 NaN\n",
       "2016-04-28 NaN\n",
       "2016-04-29   6"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.resample('D')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-04-22</th>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-23</th>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-24</th>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-25</th>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-26</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-27</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-28</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04-29</th>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             0\n",
       "2016-04-22  10\n",
       "2016-04-23  10\n",
       "2016-04-24  10\n",
       "2016-04-25  10\n",
       "2016-04-26 NaN\n",
       "2016-04-27 NaN\n",
       "2016-04-28 NaN\n",
       "2016-04-29   6"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.resample('D', fill_method='ffill', limit=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-04-25</th>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-05-02</th>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             0\n",
       "2016-04-25  10\n",
       "2016-05-02   6"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 以周为单位，每周一采样\n",
    "df.resample('W-MON', fill_method='ffill')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 时期重采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01</th>\n",
       "      <td>20</td>\n",
       "      <td>7</td>\n",
       "      <td>22</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-02</th>\n",
       "      <td>2</td>\n",
       "      <td>28</td>\n",
       "      <td>21</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-03</th>\n",
       "      <td>13</td>\n",
       "      <td>17</td>\n",
       "      <td>12</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-04</th>\n",
       "      <td>24</td>\n",
       "      <td>17</td>\n",
       "      <td>20</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-05</th>\n",
       "      <td>15</td>\n",
       "      <td>13</td>\n",
       "      <td>15</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-06</th>\n",
       "      <td>19</td>\n",
       "      <td>28</td>\n",
       "      <td>2</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-07</th>\n",
       "      <td>20</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-08</th>\n",
       "      <td>10</td>\n",
       "      <td>18</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-09</th>\n",
       "      <td>17</td>\n",
       "      <td>24</td>\n",
       "      <td>11</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10</th>\n",
       "      <td>23</td>\n",
       "      <td>2</td>\n",
       "      <td>21</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11</th>\n",
       "      <td>24</td>\n",
       "      <td>3</td>\n",
       "      <td>19</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12</th>\n",
       "      <td>7</td>\n",
       "      <td>16</td>\n",
       "      <td>6</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01</th>\n",
       "      <td>18</td>\n",
       "      <td>13</td>\n",
       "      <td>8</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-02</th>\n",
       "      <td>17</td>\n",
       "      <td>14</td>\n",
       "      <td>2</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03</th>\n",
       "      <td>17</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-04</th>\n",
       "      <td>24</td>\n",
       "      <td>14</td>\n",
       "      <td>22</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-05</th>\n",
       "      <td>16</td>\n",
       "      <td>14</td>\n",
       "      <td>20</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-06</th>\n",
       "      <td>26</td>\n",
       "      <td>29</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-07</th>\n",
       "      <td>2</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-08</th>\n",
       "      <td>12</td>\n",
       "      <td>11</td>\n",
       "      <td>17</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-09</th>\n",
       "      <td>19</td>\n",
       "      <td>21</td>\n",
       "      <td>4</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-10</th>\n",
       "      <td>21</td>\n",
       "      <td>16</td>\n",
       "      <td>11</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-11</th>\n",
       "      <td>16</td>\n",
       "      <td>23</td>\n",
       "      <td>2</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-12</th>\n",
       "      <td>21</td>\n",
       "      <td>9</td>\n",
       "      <td>27</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A   B   C   D\n",
       "2015-01  20   7  22  18\n",
       "2015-02   2  28  21  19\n",
       "2015-03  13  17  12   7\n",
       "2015-04  24  17  20  14\n",
       "2015-05  15  13  15  20\n",
       "2015-06  19  28   2  22\n",
       "2015-07  20   7   2  27\n",
       "2015-08  10  18   2  16\n",
       "2015-09  17  24  11   9\n",
       "2015-10  23   2  21  25\n",
       "2015-11  24   3  19   8\n",
       "2015-12   7  16   6  12\n",
       "2016-01  18  13   8  15\n",
       "2016-02  17  14   2  21\n",
       "2016-03  17   6   5  24\n",
       "2016-04  24  14  22  14\n",
       "2016-05  16  14  20  14\n",
       "2016-06  26  29  14  15\n",
       "2016-07   2  11  11   2\n",
       "2016-08  12  11  17  18\n",
       "2016-09  19  21   4  16\n",
       "2016-10  21  16  11   7\n",
       "2016-11  16  23   2  22\n",
       "2016-12  21   9  27  11"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randint(2, 30, (24, 4)), \n",
    "                  index=pd.period_range('2015-01', '2016-12', freq='M'),\n",
    "                  columns=list('ABCD'))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015</th>\n",
       "      <td>16.166667</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>12.750000</td>\n",
       "      <td>16.416667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016</th>\n",
       "      <td>17.416667</td>\n",
       "      <td>15.083333</td>\n",
       "      <td>11.916667</td>\n",
       "      <td>14.916667</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              A          B          C          D\n",
       "2015  16.166667  15.000000  12.750000  16.416667\n",
       "2016  17.416667  15.083333  11.916667  14.916667"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adf = df.resample('A-DEC', how='mean')\n",
    "adf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015</th>\n",
       "      <td>14.800000</td>\n",
       "      <td>16.400000</td>\n",
       "      <td>18.000000</td>\n",
       "      <td>15.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016</th>\n",
       "      <td>17.666667</td>\n",
       "      <td>13.250000</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>17.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017</th>\n",
       "      <td>16.714286</td>\n",
       "      <td>17.142857</td>\n",
       "      <td>12.285714</td>\n",
       "      <td>13.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              A          B          C      D\n",
       "2015  14.800000  16.400000  18.000000  15.60\n",
       "2016  17.666667  13.250000  10.000000  17.25\n",
       "2017  16.714286  17.142857  12.285714  13.00"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.resample('A-MAY', how='mean')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015Q1</th>\n",
       "      <td>16.166667</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>12.750000</td>\n",
       "      <td>16.416667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015Q2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015Q3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015Q4</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016Q1</th>\n",
       "      <td>17.416667</td>\n",
       "      <td>15.083333</td>\n",
       "      <td>11.916667</td>\n",
       "      <td>14.916667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016Q2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016Q3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016Q4</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                A          B          C          D\n",
       "2015Q1  16.166667  15.000000  12.750000  16.416667\n",
       "2015Q2        NaN        NaN        NaN        NaN\n",
       "2015Q3        NaN        NaN        NaN        NaN\n",
       "2015Q4        NaN        NaN        NaN        NaN\n",
       "2016Q1  17.416667  15.083333  11.916667  14.916667\n",
       "2016Q2        NaN        NaN        NaN        NaN\n",
       "2016Q3        NaN        NaN        NaN        NaN\n",
       "2016Q4        NaN        NaN        NaN        NaN"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 升采样\n",
    "adf.resample('Q-DEC')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015Q1</th>\n",
       "      <td>16.166667</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>12.750000</td>\n",
       "      <td>16.416667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015Q2</th>\n",
       "      <td>16.166667</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>12.750000</td>\n",
       "      <td>16.416667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015Q3</th>\n",
       "      <td>16.166667</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>12.750000</td>\n",
       "      <td>16.416667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015Q4</th>\n",
       "      <td>16.166667</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>12.750000</td>\n",
       "      <td>16.416667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016Q1</th>\n",
       "      <td>17.416667</td>\n",
       "      <td>15.083333</td>\n",
       "      <td>11.916667</td>\n",
       "      <td>14.916667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016Q2</th>\n",
       "      <td>17.416667</td>\n",
       "      <td>15.083333</td>\n",
       "      <td>11.916667</td>\n",
       "      <td>14.916667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016Q3</th>\n",
       "      <td>17.416667</td>\n",
       "      <td>15.083333</td>\n",
       "      <td>11.916667</td>\n",
       "      <td>14.916667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016Q4</th>\n",
       "      <td>17.416667</td>\n",
       "      <td>15.083333</td>\n",
       "      <td>11.916667</td>\n",
       "      <td>14.916667</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                A          B          C          D\n",
       "2015Q1  16.166667  15.000000  12.750000  16.416667\n",
       "2015Q2  16.166667  15.000000  12.750000  16.416667\n",
       "2015Q3  16.166667  15.000000  12.750000  16.416667\n",
       "2015Q4  16.166667  15.000000  12.750000  16.416667\n",
       "2016Q1  17.416667  15.083333  11.916667  14.916667\n",
       "2016Q2  17.416667  15.083333  11.916667  14.916667\n",
       "2016Q3  17.416667  15.083333  11.916667  14.916667\n",
       "2016Q4  17.416667  15.083333  11.916667  14.916667"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adf.resample('Q-DEC', fill_method='ffill')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 性能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000000"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = 1000000\n",
    "ts = pd.Series(np.random.randn(n), \n",
    "               index=pd.date_range('2000-01-01', periods=n, freq='10ms'))\n",
    "len(ts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 loops, best of 3: 21.9 ms per loop\n"
     ]
    }
   ],
   "source": [
    "%timeit ts.resample('10min', how='ohlc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2000-01-01</th>\n",
       "      <td>1.161091</td>\n",
       "      <td>4.551988</td>\n",
       "      <td>-4.660681</td>\n",
       "      <td>-0.406231</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                open      high       low     close\n",
       "2000-01-01  1.161091  4.551988 -4.660681 -0.406231"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts.resample('D', how='ohlc')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 从文件中读取日期序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Adj Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-12-22</th>\n",
       "      <td>16.86</td>\n",
       "      <td>17.13</td>\n",
       "      <td>16.48</td>\n",
       "      <td>16.95</td>\n",
       "      <td>13519900</td>\n",
       "      <td>16.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-21</th>\n",
       "      <td>16.31</td>\n",
       "      <td>17.00</td>\n",
       "      <td>16.20</td>\n",
       "      <td>16.85</td>\n",
       "      <td>14132200</td>\n",
       "      <td>16.85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-18</th>\n",
       "      <td>16.59</td>\n",
       "      <td>16.70</td>\n",
       "      <td>16.21</td>\n",
       "      <td>16.31</td>\n",
       "      <td>10524300</td>\n",
       "      <td>16.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-17</th>\n",
       "      <td>16.28</td>\n",
       "      <td>16.75</td>\n",
       "      <td>16.16</td>\n",
       "      <td>16.60</td>\n",
       "      <td>12326500</td>\n",
       "      <td>16.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-16</th>\n",
       "      <td>16.23</td>\n",
       "      <td>16.42</td>\n",
       "      <td>16.05</td>\n",
       "      <td>16.28</td>\n",
       "      <td>8026000</td>\n",
       "      <td>16.28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-15</th>\n",
       "      <td>16.06</td>\n",
       "      <td>16.31</td>\n",
       "      <td>15.95</td>\n",
       "      <td>16.18</td>\n",
       "      <td>6647500</td>\n",
       "      <td>16.18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-14</th>\n",
       "      <td>15.60</td>\n",
       "      <td>16.06</td>\n",
       "      <td>15.45</td>\n",
       "      <td>16.06</td>\n",
       "      <td>8355200</td>\n",
       "      <td>16.06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-11</th>\n",
       "      <td>15.50</td>\n",
       "      <td>15.80</td>\n",
       "      <td>15.41</td>\n",
       "      <td>15.62</td>\n",
       "      <td>7243500</td>\n",
       "      <td>15.62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-10</th>\n",
       "      <td>15.99</td>\n",
       "      <td>16.05</td>\n",
       "      <td>15.51</td>\n",
       "      <td>15.56</td>\n",
       "      <td>7654900</td>\n",
       "      <td>15.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-09</th>\n",
       "      <td>16.00</td>\n",
       "      <td>16.19</td>\n",
       "      <td>15.80</td>\n",
       "      <td>15.83</td>\n",
       "      <td>7926900</td>\n",
       "      <td>15.83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-08</th>\n",
       "      <td>16.54</td>\n",
       "      <td>16.55</td>\n",
       "      <td>16.01</td>\n",
       "      <td>16.05</td>\n",
       "      <td>7640100</td>\n",
       "      <td>16.05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-07</th>\n",
       "      <td>16.50</td>\n",
       "      <td>17.04</td>\n",
       "      <td>16.48</td>\n",
       "      <td>16.63</td>\n",
       "      <td>11917200</td>\n",
       "      <td>16.63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-04</th>\n",
       "      <td>16.13</td>\n",
       "      <td>16.85</td>\n",
       "      <td>16.01</td>\n",
       "      <td>16.62</td>\n",
       "      <td>14011100</td>\n",
       "      <td>16.62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-03</th>\n",
       "      <td>15.97</td>\n",
       "      <td>16.34</td>\n",
       "      <td>15.88</td>\n",
       "      <td>16.21</td>\n",
       "      <td>9504000</td>\n",
       "      <td>16.21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-02</th>\n",
       "      <td>15.89</td>\n",
       "      <td>16.04</td>\n",
       "      <td>15.50</td>\n",
       "      <td>15.88</td>\n",
       "      <td>11229600</td>\n",
       "      <td>15.88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-01</th>\n",
       "      <td>15.67</td>\n",
       "      <td>15.96</td>\n",
       "      <td>15.50</td>\n",
       "      <td>15.85</td>\n",
       "      <td>7192200</td>\n",
       "      <td>15.85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-30</th>\n",
       "      <td>15.54</td>\n",
       "      <td>15.90</td>\n",
       "      <td>15.05</td>\n",
       "      <td>15.70</td>\n",
       "      <td>11615200</td>\n",
       "      <td>15.70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-27</th>\n",
       "      <td>16.61</td>\n",
       "      <td>16.99</td>\n",
       "      <td>15.10</td>\n",
       "      <td>15.54</td>\n",
       "      <td>15177000</td>\n",
       "      <td>15.54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-26</th>\n",
       "      <td>16.98</td>\n",
       "      <td>17.22</td>\n",
       "      <td>16.62</td>\n",
       "      <td>16.78</td>\n",
       "      <td>13196300</td>\n",
       "      <td>16.78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-25</th>\n",
       "      <td>16.15</td>\n",
       "      <td>17.04</td>\n",
       "      <td>16.03</td>\n",
       "      <td>16.94</td>\n",
       "      <td>18600100</td>\n",
       "      <td>16.94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-24</th>\n",
       "      <td>15.90</td>\n",
       "      <td>16.20</td>\n",
       "      <td>15.70</td>\n",
       "      <td>16.15</td>\n",
       "      <td>8561200</td>\n",
       "      <td>16.15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-23</th>\n",
       "      <td>16.09</td>\n",
       "      <td>16.32</td>\n",
       "      <td>16.00</td>\n",
       "      <td>16.05</td>\n",
       "      <td>9441700</td>\n",
       "      <td>16.05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-20</th>\n",
       "      <td>15.96</td>\n",
       "      <td>16.17</td>\n",
       "      <td>15.81</td>\n",
       "      <td>16.08</td>\n",
       "      <td>8022200</td>\n",
       "      <td>16.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-19</th>\n",
       "      <td>15.75</td>\n",
       "      <td>16.05</td>\n",
       "      <td>15.71</td>\n",
       "      <td>16.02</td>\n",
       "      <td>5193300</td>\n",
       "      <td>16.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-18</th>\n",
       "      <td>16.26</td>\n",
       "      <td>16.30</td>\n",
       "      <td>15.72</td>\n",
       "      <td>15.75</td>\n",
       "      <td>7318500</td>\n",
       "      <td>15.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-17</th>\n",
       "      <td>16.41</td>\n",
       "      <td>16.47</td>\n",
       "      <td>16.11</td>\n",
       "      <td>16.22</td>\n",
       "      <td>11479800</td>\n",
       "      <td>16.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-16</th>\n",
       "      <td>15.70</td>\n",
       "      <td>16.22</td>\n",
       "      <td>15.61</td>\n",
       "      <td>16.21</td>\n",
       "      <td>9083200</td>\n",
       "      <td>16.21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-13</th>\n",
       "      <td>16.36</td>\n",
       "      <td>16.47</td>\n",
       "      <td>15.90</td>\n",
       "      <td>15.95</td>\n",
       "      <td>12924400</td>\n",
       "      <td>15.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-12</th>\n",
       "      <td>16.23</td>\n",
       "      <td>16.92</td>\n",
       "      <td>16.00</td>\n",
       "      <td>16.59</td>\n",
       "      <td>16492800</td>\n",
       "      <td>16.59</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-11</th>\n",
       "      <td>16.16</td>\n",
       "      <td>16.28</td>\n",
       "      <td>15.81</td>\n",
       "      <td>16.22</td>\n",
       "      <td>15661900</td>\n",
       "      <td>16.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10</th>\n",
       "      <td>16.29</td>\n",
       "      <td>16.69</td>\n",
       "      <td>16.04</td>\n",
       "      <td>16.15</td>\n",
       "      <td>21457600</td>\n",
       "      <td>16.15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-09</th>\n",
       "      <td>15.70</td>\n",
       "      <td>16.29</td>\n",
       "      <td>15.56</td>\n",
       "      <td>16.02</td>\n",
       "      <td>20842600</td>\n",
       "      <td>16.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-06</th>\n",
       "      <td>15.53</td>\n",
       "      <td>16.01</td>\n",
       "      <td>15.41</td>\n",
       "      <td>15.86</td>\n",
       "      <td>17735800</td>\n",
       "      <td>15.86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-05</th>\n",
       "      <td>15.33</td>\n",
       "      <td>15.79</td>\n",
       "      <td>15.21</td>\n",
       "      <td>15.52</td>\n",
       "      <td>19051400</td>\n",
       "      <td>15.52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-04</th>\n",
       "      <td>14.65</td>\n",
       "      <td>15.35</td>\n",
       "      <td>14.65</td>\n",
       "      <td>15.33</td>\n",
       "      <td>14578200</td>\n",
       "      <td>15.33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-03</th>\n",
       "      <td>14.84</td>\n",
       "      <td>14.96</td>\n",
       "      <td>14.44</td>\n",
       "      <td>14.62</td>\n",
       "      <td>6576300</td>\n",
       "      <td>14.62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-02</th>\n",
       "      <td>14.91</td>\n",
       "      <td>15.18</td>\n",
       "      <td>14.74</td>\n",
       "      <td>14.74</td>\n",
       "      <td>9487800</td>\n",
       "      <td>14.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-30</th>\n",
       "      <td>15.25</td>\n",
       "      <td>15.52</td>\n",
       "      <td>14.81</td>\n",
       "      <td>15.22</td>\n",
       "      <td>12908500</td>\n",
       "      <td>15.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-29</th>\n",
       "      <td>15.01</td>\n",
       "      <td>15.36</td>\n",
       "      <td>14.96</td>\n",
       "      <td>15.30</td>\n",
       "      <td>11177100</td>\n",
       "      <td>15.30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-28</th>\n",
       "      <td>15.14</td>\n",
       "      <td>15.50</td>\n",
       "      <td>14.96</td>\n",
       "      <td>15.02</td>\n",
       "      <td>11373200</td>\n",
       "      <td>15.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-27</th>\n",
       "      <td>15.10</td>\n",
       "      <td>15.17</td>\n",
       "      <td>14.51</td>\n",
       "      <td>15.15</td>\n",
       "      <td>12950400</td>\n",
       "      <td>15.15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-26</th>\n",
       "      <td>15.41</td>\n",
       "      <td>15.55</td>\n",
       "      <td>14.87</td>\n",
       "      <td>15.18</td>\n",
       "      <td>15844500</td>\n",
       "      <td>15.18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-23</th>\n",
       "      <td>14.80</td>\n",
       "      <td>15.23</td>\n",
       "      <td>14.75</td>\n",
       "      <td>15.20</td>\n",
       "      <td>14769000</td>\n",
       "      <td>15.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-22</th>\n",
       "      <td>14.28</td>\n",
       "      <td>14.82</td>\n",
       "      <td>14.25</td>\n",
       "      <td>14.73</td>\n",
       "      <td>10428900</td>\n",
       "      <td>14.73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-21</th>\n",
       "      <td>15.24</td>\n",
       "      <td>15.70</td>\n",
       "      <td>14.08</td>\n",
       "      <td>14.26</td>\n",
       "      <td>21113500</td>\n",
       "      <td>14.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-20</th>\n",
       "      <td>14.99</td>\n",
       "      <td>15.24</td>\n",
       "      <td>14.89</td>\n",
       "      <td>15.22</td>\n",
       "      <td>11935800</td>\n",
       "      <td>15.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-19</th>\n",
       "      <td>15.27</td>\n",
       "      <td>15.35</td>\n",
       "      <td>14.85</td>\n",
       "      <td>15.03</td>\n",
       "      <td>11601300</td>\n",
       "      <td>15.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-16</th>\n",
       "      <td>15.23</td>\n",
       "      <td>15.35</td>\n",
       "      <td>14.82</td>\n",
       "      <td>15.25</td>\n",
       "      <td>14168700</td>\n",
       "      <td>15.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-15</th>\n",
       "      <td>14.73</td>\n",
       "      <td>15.15</td>\n",
       "      <td>14.60</td>\n",
       "      <td>15.12</td>\n",
       "      <td>11177700</td>\n",
       "      <td>15.12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-14</th>\n",
       "      <td>14.99</td>\n",
       "      <td>15.12</td>\n",
       "      <td>14.72</td>\n",
       "      <td>14.73</td>\n",
       "      <td>10368900</td>\n",
       "      <td>14.73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-13</th>\n",
       "      <td>15.02</td>\n",
       "      <td>15.19</td>\n",
       "      <td>14.85</td>\n",
       "      <td>15.07</td>\n",
       "      <td>13408200</td>\n",
       "      <td>15.07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-12</th>\n",
       "      <td>14.63</td>\n",
       "      <td>15.43</td>\n",
       "      <td>14.41</td>\n",
       "      <td>15.30</td>\n",
       "      <td>24110800</td>\n",
       "      <td>15.30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-09</th>\n",
       "      <td>14.50</td>\n",
       "      <td>14.79</td>\n",
       "      <td>14.11</td>\n",
       "      <td>14.62</td>\n",
       "      <td>23818500</td>\n",
       "      <td>14.62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-08</th>\n",
       "      <td>14.75</td>\n",
       "      <td>14.75</td>\n",
       "      <td>14.65</td>\n",
       "      <td>14.75</td>\n",
       "      <td>18317200</td>\n",
       "      <td>14.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-07</th>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>0</td>\n",
       "      <td>13.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-06</th>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>0</td>\n",
       "      <td>13.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-05</th>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>0</td>\n",
       "      <td>13.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-02</th>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>0</td>\n",
       "      <td>13.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-01</th>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>0</td>\n",
       "      <td>13.41</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             Open   High    Low  Close    Volume  Adj Close\n",
       "Date                                                       \n",
       "2015-12-22  16.86  17.13  16.48  16.95  13519900      16.95\n",
       "2015-12-21  16.31  17.00  16.20  16.85  14132200      16.85\n",
       "2015-12-18  16.59  16.70  16.21  16.31  10524300      16.31\n",
       "2015-12-17  16.28  16.75  16.16  16.60  12326500      16.60\n",
       "2015-12-16  16.23  16.42  16.05  16.28   8026000      16.28\n",
       "2015-12-15  16.06  16.31  15.95  16.18   6647500      16.18\n",
       "2015-12-14  15.60  16.06  15.45  16.06   8355200      16.06\n",
       "2015-12-11  15.50  15.80  15.41  15.62   7243500      15.62\n",
       "2015-12-10  15.99  16.05  15.51  15.56   7654900      15.56\n",
       "2015-12-09  16.00  16.19  15.80  15.83   7926900      15.83\n",
       "2015-12-08  16.54  16.55  16.01  16.05   7640100      16.05\n",
       "2015-12-07  16.50  17.04  16.48  16.63  11917200      16.63\n",
       "2015-12-04  16.13  16.85  16.01  16.62  14011100      16.62\n",
       "2015-12-03  15.97  16.34  15.88  16.21   9504000      16.21\n",
       "2015-12-02  15.89  16.04  15.50  15.88  11229600      15.88\n",
       "2015-12-01  15.67  15.96  15.50  15.85   7192200      15.85\n",
       "2015-11-30  15.54  15.90  15.05  15.70  11615200      15.70\n",
       "2015-11-27  16.61  16.99  15.10  15.54  15177000      15.54\n",
       "2015-11-26  16.98  17.22  16.62  16.78  13196300      16.78\n",
       "2015-11-25  16.15  17.04  16.03  16.94  18600100      16.94\n",
       "2015-11-24  15.90  16.20  15.70  16.15   8561200      16.15\n",
       "2015-11-23  16.09  16.32  16.00  16.05   9441700      16.05\n",
       "2015-11-20  15.96  16.17  15.81  16.08   8022200      16.08\n",
       "2015-11-19  15.75  16.05  15.71  16.02   5193300      16.02\n",
       "2015-11-18  16.26  16.30  15.72  15.75   7318500      15.75\n",
       "2015-11-17  16.41  16.47  16.11  16.22  11479800      16.22\n",
       "2015-11-16  15.70  16.22  15.61  16.21   9083200      16.21\n",
       "2015-11-13  16.36  16.47  15.90  15.95  12924400      15.95\n",
       "2015-11-12  16.23  16.92  16.00  16.59  16492800      16.59\n",
       "2015-11-11  16.16  16.28  15.81  16.22  15661900      16.22\n",
       "2015-11-10  16.29  16.69  16.04  16.15  21457600      16.15\n",
       "2015-11-09  15.70  16.29  15.56  16.02  20842600      16.02\n",
       "2015-11-06  15.53  16.01  15.41  15.86  17735800      15.86\n",
       "2015-11-05  15.33  15.79  15.21  15.52  19051400      15.52\n",
       "2015-11-04  14.65  15.35  14.65  15.33  14578200      15.33\n",
       "2015-11-03  14.84  14.96  14.44  14.62   6576300      14.62\n",
       "2015-11-02  14.91  15.18  14.74  14.74   9487800      14.74\n",
       "2015-10-30  15.25  15.52  14.81  15.22  12908500      15.22\n",
       "2015-10-29  15.01  15.36  14.96  15.30  11177100      15.30\n",
       "2015-10-28  15.14  15.50  14.96  15.02  11373200      15.02\n",
       "2015-10-27  15.10  15.17  14.51  15.15  12950400      15.15\n",
       "2015-10-26  15.41  15.55  14.87  15.18  15844500      15.18\n",
       "2015-10-23  14.80  15.23  14.75  15.20  14769000      15.20\n",
       "2015-10-22  14.28  14.82  14.25  14.73  10428900      14.73\n",
       "2015-10-21  15.24  15.70  14.08  14.26  21113500      14.26\n",
       "2015-10-20  14.99  15.24  14.89  15.22  11935800      15.22\n",
       "2015-10-19  15.27  15.35  14.85  15.03  11601300      15.03\n",
       "2015-10-16  15.23  15.35  14.82  15.25  14168700      15.25\n",
       "2015-10-15  14.73  15.15  14.60  15.12  11177700      15.12\n",
       "2015-10-14  14.99  15.12  14.72  14.73  10368900      14.73\n",
       "2015-10-13  15.02  15.19  14.85  15.07  13408200      15.07\n",
       "2015-10-12  14.63  15.43  14.41  15.30  24110800      15.30\n",
       "2015-10-09  14.50  14.79  14.11  14.62  23818500      14.62\n",
       "2015-10-08  14.75  14.75  14.65  14.75  18317200      14.75\n",
       "2015-10-07  13.41  13.41  13.41  13.41         0      13.41\n",
       "2015-10-06  13.41  13.41  13.41  13.41         0      13.41\n",
       "2015-10-05  13.41  13.41  13.41  13.41         0      13.41\n",
       "2015-10-02  13.41  13.41  13.41  13.41         0      13.41\n",
       "2015-10-01  13.41  13.41  13.41  13.41         0      13.41"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('data/002001.csv', index_col='Date')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index([u'2015-12-22', u'2015-12-21', u'2015-12-18', u'2015-12-17',\n",
       "       u'2015-12-16', u'2015-12-15', u'2015-12-14', u'2015-12-11',\n",
       "       u'2015-12-10', u'2015-12-09', u'2015-12-08', u'2015-12-07',\n",
       "       u'2015-12-04', u'2015-12-03', u'2015-12-02', u'2015-12-01',\n",
       "       u'2015-11-30', u'2015-11-27', u'2015-11-26', u'2015-11-25',\n",
       "       u'2015-11-24', u'2015-11-23', u'2015-11-20', u'2015-11-19',\n",
       "       u'2015-11-18', u'2015-11-17', u'2015-11-16', u'2015-11-13',\n",
       "       u'2015-11-12', u'2015-11-11', u'2015-11-10', u'2015-11-09',\n",
       "       u'2015-11-06', u'2015-11-05', u'2015-11-04', u'2015-11-03',\n",
       "       u'2015-11-02', u'2015-10-30', u'2015-10-29', u'2015-10-28',\n",
       "       u'2015-10-27', u'2015-10-26', u'2015-10-23', u'2015-10-22',\n",
       "       u'2015-10-21', u'2015-10-20', u'2015-10-19', u'2015-10-16',\n",
       "       u'2015-10-15', u'2015-10-14', u'2015-10-13', u'2015-10-12',\n",
       "       u'2015-10-09', u'2015-10-08', u'2015-10-07', u'2015-10-06',\n",
       "       u'2015-10-05', u'2015-10-02', u'2015-10-01'],\n",
       "      dtype='object', name=u'Date')"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2015-12-22', '2015-12-21', '2015-12-18', '2015-12-17',\n",
       "               '2015-12-16', '2015-12-15', '2015-12-14', '2015-12-11',\n",
       "               '2015-12-10', '2015-12-09', '2015-12-08', '2015-12-07',\n",
       "               '2015-12-04', '2015-12-03', '2015-12-02', '2015-12-01',\n",
       "               '2015-11-30', '2015-11-27', '2015-11-26', '2015-11-25',\n",
       "               '2015-11-24', '2015-11-23', '2015-11-20', '2015-11-19',\n",
       "               '2015-11-18', '2015-11-17', '2015-11-16', '2015-11-13',\n",
       "               '2015-11-12', '2015-11-11', '2015-11-10', '2015-11-09',\n",
       "               '2015-11-06', '2015-11-05', '2015-11-04', '2015-11-03',\n",
       "               '2015-11-02', '2015-10-30', '2015-10-29', '2015-10-28',\n",
       "               '2015-10-27', '2015-10-26', '2015-10-23', '2015-10-22',\n",
       "               '2015-10-21', '2015-10-20', '2015-10-19', '2015-10-16',\n",
       "               '2015-10-15', '2015-10-14', '2015-10-13', '2015-10-12',\n",
       "               '2015-10-09', '2015-10-08', '2015-10-07', '2015-10-06',\n",
       "               '2015-10-05', '2015-10-02', '2015-10-01'],\n",
       "              dtype='datetime64[ns]', name=u'Date', freq=None)"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('data/002001.csv', index_col='Date', parse_dates=True)\n",
    "df.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-10-02</th>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-09</th>\n",
       "      <td>13.41</td>\n",
       "      <td>14.75</td>\n",
       "      <td>13.41</td>\n",
       "      <td>14.62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-16</th>\n",
       "      <td>15.30</td>\n",
       "      <td>15.30</td>\n",
       "      <td>14.73</td>\n",
       "      <td>15.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-23</th>\n",
       "      <td>15.03</td>\n",
       "      <td>15.22</td>\n",
       "      <td>14.26</td>\n",
       "      <td>15.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-30</th>\n",
       "      <td>15.18</td>\n",
       "      <td>15.30</td>\n",
       "      <td>15.02</td>\n",
       "      <td>15.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-06</th>\n",
       "      <td>14.74</td>\n",
       "      <td>15.86</td>\n",
       "      <td>14.62</td>\n",
       "      <td>15.86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-13</th>\n",
       "      <td>16.02</td>\n",
       "      <td>16.59</td>\n",
       "      <td>15.95</td>\n",
       "      <td>15.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-20</th>\n",
       "      <td>16.21</td>\n",
       "      <td>16.22</td>\n",
       "      <td>15.75</td>\n",
       "      <td>16.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-27</th>\n",
       "      <td>16.05</td>\n",
       "      <td>16.94</td>\n",
       "      <td>15.54</td>\n",
       "      <td>15.54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-04</th>\n",
       "      <td>15.70</td>\n",
       "      <td>16.62</td>\n",
       "      <td>15.70</td>\n",
       "      <td>16.62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-11</th>\n",
       "      <td>16.63</td>\n",
       "      <td>16.63</td>\n",
       "      <td>15.56</td>\n",
       "      <td>15.62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-18</th>\n",
       "      <td>16.06</td>\n",
       "      <td>16.60</td>\n",
       "      <td>16.06</td>\n",
       "      <td>16.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-25</th>\n",
       "      <td>16.85</td>\n",
       "      <td>16.95</td>\n",
       "      <td>16.85</td>\n",
       "      <td>16.95</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             open   high    low  close\n",
       "Date                                  \n",
       "2015-10-02  13.41  13.41  13.41  13.41\n",
       "2015-10-09  13.41  14.75  13.41  14.62\n",
       "2015-10-16  15.30  15.30  14.73  15.25\n",
       "2015-10-23  15.03  15.22  14.26  15.20\n",
       "2015-10-30  15.18  15.30  15.02  15.22\n",
       "2015-11-06  14.74  15.86  14.62  15.86\n",
       "2015-11-13  16.02  16.59  15.95  15.95\n",
       "2015-11-20  16.21  16.22  15.75  16.08\n",
       "2015-11-27  16.05  16.94  15.54  15.54\n",
       "2015-12-04  15.70  16.62  15.70  16.62\n",
       "2015-12-11  16.63  16.63  15.56  15.62\n",
       "2015-12-18  16.06  16.60  16.06  16.31\n",
       "2015-12-25  16.85  16.95  16.85  16.95"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wdf = df['Adj Close'].resample('W-FRI', how='ohlc')\n",
    "wdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>Volume</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-10-02</th>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>13.41</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-09</th>\n",
       "      <td>13.41</td>\n",
       "      <td>14.75</td>\n",
       "      <td>13.41</td>\n",
       "      <td>14.62</td>\n",
       "      <td>42135700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-16</th>\n",
       "      <td>15.30</td>\n",
       "      <td>15.30</td>\n",
       "      <td>14.73</td>\n",
       "      <td>15.25</td>\n",
       "      <td>73234300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-23</th>\n",
       "      <td>15.03</td>\n",
       "      <td>15.22</td>\n",
       "      <td>14.26</td>\n",
       "      <td>15.20</td>\n",
       "      <td>69848500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-10-30</th>\n",
       "      <td>15.18</td>\n",
       "      <td>15.30</td>\n",
       "      <td>15.02</td>\n",
       "      <td>15.22</td>\n",
       "      <td>64253700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-06</th>\n",
       "      <td>14.74</td>\n",
       "      <td>15.86</td>\n",
       "      <td>14.62</td>\n",
       "      <td>15.86</td>\n",
       "      <td>67429500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-13</th>\n",
       "      <td>16.02</td>\n",
       "      <td>16.59</td>\n",
       "      <td>15.95</td>\n",
       "      <td>15.95</td>\n",
       "      <td>87379300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-20</th>\n",
       "      <td>16.21</td>\n",
       "      <td>16.22</td>\n",
       "      <td>15.75</td>\n",
       "      <td>16.08</td>\n",
       "      <td>41097000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-27</th>\n",
       "      <td>16.05</td>\n",
       "      <td>16.94</td>\n",
       "      <td>15.54</td>\n",
       "      <td>15.54</td>\n",
       "      <td>64976300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-04</th>\n",
       "      <td>15.70</td>\n",
       "      <td>16.62</td>\n",
       "      <td>15.70</td>\n",
       "      <td>16.62</td>\n",
       "      <td>53552100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-11</th>\n",
       "      <td>16.63</td>\n",
       "      <td>16.63</td>\n",
       "      <td>15.56</td>\n",
       "      <td>15.62</td>\n",
       "      <td>42382600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-18</th>\n",
       "      <td>16.06</td>\n",
       "      <td>16.60</td>\n",
       "      <td>16.06</td>\n",
       "      <td>16.31</td>\n",
       "      <td>45879500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-25</th>\n",
       "      <td>16.85</td>\n",
       "      <td>16.95</td>\n",
       "      <td>16.85</td>\n",
       "      <td>16.95</td>\n",
       "      <td>27652100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             open   high    low  close    Volume\n",
       "Date                                            \n",
       "2015-10-02  13.41  13.41  13.41  13.41         0\n",
       "2015-10-09  13.41  14.75  13.41  14.62  42135700\n",
       "2015-10-16  15.30  15.30  14.73  15.25  73234300\n",
       "2015-10-23  15.03  15.22  14.26  15.20  69848500\n",
       "2015-10-30  15.18  15.30  15.02  15.22  64253700\n",
       "2015-11-06  14.74  15.86  14.62  15.86  67429500\n",
       "2015-11-13  16.02  16.59  15.95  15.95  87379300\n",
       "2015-11-20  16.21  16.22  15.75  16.08  41097000\n",
       "2015-11-27  16.05  16.94  15.54  15.54  64976300\n",
       "2015-12-04  15.70  16.62  15.70  16.62  53552100\n",
       "2015-12-11  16.63  16.63  15.56  15.62  42382600\n",
       "2015-12-18  16.06  16.60  16.06  16.31  45879500\n",
       "2015-12-25  16.85  16.95  16.85  16.95  27652100"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wdf['Volume'] = df['Volume'].resample('W-FRI', how='sum')\n",
    "wdf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 自定义时间日期解析函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-01-01</th>\n",
       "      <td>10.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-02</th>\n",
       "      <td>10.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-03</th>\n",
       "      <td>10.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-04</th>\n",
       "      <td>10.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-05</th>\n",
       "      <td>11.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-06</th>\n",
       "      <td>10.6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Price\n",
       "Date             \n",
       "2016-01-01   10.2\n",
       "2016-01-02   10.4\n",
       "2016-01-03   10.5\n",
       "2016-01-04   10.8\n",
       "2016-01-05   11.2\n",
       "2016-01-06   10.6"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def date_parser(s):\n",
    "    s = '2016/' + s\n",
    "    d = datetime.strptime(s, '%Y/%m/%d')\n",
    "    return d\n",
    "\n",
    "df = pd.read_csv('data/custom_date.csv', parse_dates=True, index_col='Date', date_parser=date_parser)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-01-01', '2016-01-02', '2016-01-03', '2016-01-04',\n",
       "               '2016-01-05', '2016-01-06'],\n",
       "              dtype='datetime64[ns]', name=u'Date', freq=None)"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
